<?php
// +----------------------------------------------------------------------
// | Qbt CMS Plugin
// +----------------------------------------------------------------------
// | Copyright (c) 2012-2018 http://www.qbt8.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 黄老邪 <develop02@qbt8.com>
// +----------------------------------------------------------------------
namespace plugins\qbt_order;
use cmf\lib\Plugin;
use think\Config;
use think\db;

class QbtOrderPlugin extends Plugin
{

    public $info = [
        'name'        => 'QbtOrder',
        'title'       => '订单管理系统',
        'description' => '千佰特订单管理',
        'status'      => 1,
        'author'      => 'chuzeng',
        'version'     => '1.0',
        'demo_url'    => 'http://www.qbt8.com',
        'author_url'  => 'http://www.qbt8.com'
    ];

    public $hasAdmin = 1;//插件是否有后台管理界面

    // 插件安装
    public function install()
    {
        $config = Config::get('database');
        Db::query("
            CREATE TABLE IF NOT EXISTS `{$config['prefix']}order_info` (
              `order_id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
              `order_sn` varchar(20) NOT NULL DEFAULT '' COMMENT '订单号',
              `user_id` mediumint(8) unsigned NOT NULL DEFAULT '0' COMMENT '用户ID',
              `order_status` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '订单状态 0：未确认，1：已确认，2：已取消',
              `shipping_status` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '发货状态 0：未发货，1：已发货，2：已确认收货',
              `pay_status` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '支付状态 0：未付款，2：已付款',
              `consignee` varchar(60) NOT NULL DEFAULT '' COMMENT '收货人',
              `country` smallint(5) unsigned NOT NULL DEFAULT '1' COMMENT '国家',
              `province` varchar(100) NOT NULL DEFAULT '0' COMMENT '省份',
              `city` varchar(100) NOT NULL DEFAULT '0' COMMENT '城市',
              `district` varchar(100) NOT NULL DEFAULT '0' COMMENT '镇/区',
              `address` varchar(255) NOT NULL DEFAULT '' COMMENT '详细地址',
              `zipcode` varchar(60) DEFAULT NULL COMMENT '邮政编码',
              `tel` varchar(60) NOT NULL DEFAULT '' COMMENT '电话号码',
              `email` varchar(60) NOT NULL DEFAULT '' COMMENT '邮箱',
              `postscript` varchar(255) NOT NULL DEFAULT '' COMMENT '订单评论',
              `shipping_id` tinyint(3) NOT NULL DEFAULT '1' COMMENT '配送方式id',
              `shipping_name` varchar(120) NOT NULL DEFAULT '送货上门' COMMENT '配送方式',
              `pay_name` varchar(120) NOT NULL DEFAULT '微信支付' COMMENT '支付方式',
              `goods_amount` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '总金额',
              `shipping_fee` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '快递费用',
              `insure_fee` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '保险费用',
              `pay_fee` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '支付费用',
              `pack_fee` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '包装费用',
              `order_amount` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '应付金额',
              `add_time` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '添加时间',
              `confirm_time` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '确认收货时间',
              `pay_time` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '支付时间',
              `shipping_time` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '发货时间',
              `invoice_no` varchar(255) NOT NULL DEFAULT '' COMMENT '发货单号',
              `extension_code` varchar(30) NOT NULL DEFAULT '',
              `extension_id` mediumint(8) unsigned NOT NULL DEFAULT '0',
              `has_comment` tinyint(1) unsigned DEFAULT '0' COMMENT '是否评论过',
              PRIMARY KEY (`order_id`),
              UNIQUE KEY `order_sn` (`order_sn`),
              KEY `user_id` (`user_id`),
              KEY `order_status` (`order_status`),
              KEY `shipping_status` (`shipping_status`),
              KEY `pay_status` (`pay_status`),
              KEY `shipping_id` (`shipping_id`),
              KEY `extension_code` (`extension_code`,`extension_id`)
            ) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
        ");

        DB::query("
          CREATE TABLE IF NOT EXISTS `{$config['prefix']}order_goods` (
            `rec_id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
            `order_id` mediumint(8) unsigned NOT NULL DEFAULT '0' COMMENT '订单ID',
            `goods_id` mediumint(8) unsigned NOT NULL DEFAULT '0' COMMENT '商品ID',
            `goods_name` varchar(120) NOT NULL DEFAULT '' COMMENT '商品名',
            `goods_sn` varchar(60) NOT NULL DEFAULT '' COMMENT '商品货号',
            `product_id` mediumint(8) unsigned NOT NULL DEFAULT '0',
            `goods_number` smallint(5) unsigned NOT NULL DEFAULT '1' COMMENT '商品数量',
            `market_price` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '市场价格',
            `goods_price` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '本店价格',
            `is_real` tinyint(1) unsigned NOT NULL DEFAULT '1' COMMENT '是否为实体商品',
            `diy_goods_sn` varchar(255) DEFAULT NULL COMMENT '自定义产品编号',
            PRIMARY KEY (`rec_id`),
            KEY `order_id` (`order_id`),
            KEY `goods_id` (`goods_id`)
          ) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
        ");

        DB::query("
          CREATE TABLE IF NOT EXISTS `{$config['prefix']}cart` (
            `cart_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '购物车id',
            `buyer_id` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '买家id',
            `store_id` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '店铺id',
            `store_name` varchar(50) NOT NULL DEFAULT '' COMMENT '店铺名称',
            `goods_id` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '商品id',
            `goods_name` varchar(100) NOT NULL COMMENT '商品名称',
            `goods_price` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '商品价格',
            `goods_num` smallint(5) unsigned NOT NULL DEFAULT '1' COMMENT '购买商品数量',
            `goods_image` varchar(100) NOT NULL COMMENT '商品图片',
            `bl_id` mediumint(8) unsigned NOT NULL DEFAULT '0' COMMENT '组合套装ID',
            `goods_sn` varchar(255) DEFAULT NULL COMMENT '商品货号',
            PRIMARY KEY (`cart_id`),
            KEY `member_id` (`buyer_id`),
            KEY `goods_id` (`goods_id`)
          ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='购物车数据表';
        ");
        DB::query("
          CREATE TABLE IF NOT EXISTS `{$config['prefix']}shipping` (
            `shipping_id` tinyint(3) unsigned NOT NULL AUTO_INCREMENT,
            `shipping_code` varchar(20) NOT NULL DEFAULT '',
            `shipping_name` varchar(120) NOT NULL DEFAULT '',
            `shipping_desc` varchar(255) NOT NULL DEFAULT '',
            `insure` varchar(10) NOT NULL DEFAULT '0',
            `support_cod` tinyint(1) unsigned NOT NULL DEFAULT '0',
            `enabled` tinyint(1) unsigned NOT NULL DEFAULT '0',
            `shipping_print` text NOT NULL,
            `print_bg` varchar(255) DEFAULT NULL,
            `config_lable` text,
            `print_model` tinyint(1) DEFAULT '0',
            `shipping_order` tinyint(3) unsigned NOT NULL DEFAULT '0',
            PRIMARY KEY (`shipping_id`),
            KEY `shipping_code` (`shipping_code`,`enabled`)
          ) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
          
        ");
        // DB::query("
        //     INSERT INTO `qbtcmf_shipping` VALUES ('songhuo','送货上门','送货上门','0',0,1,'1',NULL,'',0,0);
        // ");

        DB::query("
          CREATE TABLE IF NOT EXISTS `{$config['prefix']}address` (
            `address_id` mediumint(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '地址ID',
            `member_id` mediumint(9) unsigned NOT NULL DEFAULT '0' COMMENT '发货人ID',
            `true_name` varchar(50) NOT NULL DEFAULT '' COMMENT '收货人',
            `county` varchar(120) DEFAULT NULL COMMENT '省',
            `city` varchar(125) DEFAULT NULL COMMENT '市',
            `area_info` varchar(255) NOT NULL DEFAULT '' COMMENT '地区内容',
            `address` varchar(255) NOT NULL COMMENT '地址',
            `mob_phone` varchar(15) DEFAULT NULL COMMENT '电话号码',
            `is_default` enum('0','1') NOT NULL DEFAULT '0' COMMENT '1默认收货地址',
            PRIMARY KEY (`address_id`),
            KEY `member_id` (`member_id`)
          ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='买家地址信息表';
        ");
        $order_data = [
            ['order_sn' => '1', 'user_id' => '1', 'consignee' => '测试人员1', 'city' => '广东', 'consignee' => '深圳', 'district' => '福田', 'address' => '测试地址1', 'tel' => '测试电话1', 'goods_amount' => '1', 'order_amount' => '1',],
            ['order_sn' => '2', 'user_id' => '2', 'consignee' => '测试人员2', 'city' => '广东', 'consignee' => '深圳', 'district' => '福田', 'address' => '测试地址2', 'tel' => '测试电话2', 'goods_amount' => '2', 'order_amount' => '2',],
            ['order_sn' => '3', 'user_id' => '3', 'consignee' => '测试人员3', 'city' => '广东', 'consignee' => '深圳', 'district' => '福田', 'address' => '测试地址3', 'tel' => '测试电话3', 'goods_amount' => '3', 'order_amount' => '3',],
            ['order_sn' => '4', 'user_id' => '4', 'consignee' => '测试人员4', 'city' => '广东', 'consignee' => '深圳', 'district' => '福田', 'address' => '测试地址4', 'tel' => '测试电话4', 'goods_amount' => '4', 'order_amount' => '4',],
        ];
        Db::name('order_info')->insertAll($order_data);        
        return true;
    }

    // 插件卸载
    public function uninstall()
    {
        $config = Config::get('database');
        Db::query("DROP TABLE `{$config['prefix']}order_info`");
        Db::query("DROP TABLE `{$config['prefix']}order_goods`");
        Db::query("DROP TABLE `{$config['prefix']}shipping`");
        return true;
    }
}